package io.bluerain.android.sdk.update.core;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import io.bluerain.android.sdk.update.core.entities.Log;
import io.bluerain.android.sdk.update.core.service.UpdateService;
import io.bluerain.android.sdk.update.core.utils.AttachUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by foredawn on 15-9-21.
 * 提供继承的Activity
 */
public abstract class UpMainActivity extends Activity {

    private static int WHAT_ALERT_UPDATE = 0x1;
    private Log upLog;
    private Handler handler;
    private UpdateService.UpBinder upBinder;
    ServiceConnection conn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Main.init(this);
        Intent update = new Intent(UpMainActivity.this, UpdateService.class);
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                upBinder = (UpdateService.UpBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(update, conn, Service.BIND_AUTO_CREATE);
        startService(update);

        final Timer upTimer = new Timer();
        upTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (upBinder == null || upBinder.isLastVersion() == null) {
                    return;
                }
                if (upBinder.isLastVersion()) //是最终版本取消定时器
                    upTimer.cancel();
                else {
                    upLog = upBinder.getUpdateLog();
                    unbindService(conn); //解除更新服务的绑定
                    upBinder = null; //释放内存
                    upTimer.cancel();
                    //提示更新
                    handler.sendEmptyMessage(WHAT_ALERT_UPDATE);
                }

            }
        }, 0, 1000);

        //更新UI
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == WHAT_ALERT_UPDATE) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(UpMainActivity.this)
                            .setTitle("发现新版本:" + upLog.getVersion()
                                    + "\n" + "当前版本:" + AttachUtils.getCurVersion(UpMainActivity.this))
                            .setMessage("强烈建议您更新到新版，请确认更新。\n若出现签名冲突请卸载旧版再安装");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Uri uri = Uri.parse(upLog.getDownloadUrl());
                            Intent downloadIntent = new Intent(Intent.ACTION_VIEW, uri);
                            startActivity(downloadIntent);
                        }
                    }).create().show();
                }
            }
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (upBinder != null)
            unbindService(conn);
    }
}
